package com.jasonli.controller;

import com.jasonli.domain.OrderInfo;
import com.jasonli.domain.ProductInfo;
import com.jasonli.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @PostMapping
    public Result save(@RequestBody OrderInfo info) {
        boolean flag = orderInfoService.save(info);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @GetMapping("/{cid}")
    public Result getByPid(@PathVariable Integer cid) {
        List<OrderInfo> info = orderInfoService.getByCid(cid);
        Integer code = info != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, info);
    }

    @GetMapping
    public Result getAll() {
        List<OrderInfo> infoList = orderInfoService.getAll();
        Integer code = infoList != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, infoList);
    }

}
